package com.util
{
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.ComboBox;
	
	public class ComboBoxHelper
	{
		public function ComboBoxHelper()
		{
		}
		
		/**
		 *本函数用来寻找ComboBox中是否存在data与传递的data相匹配的记录，
		 * 如果有，则返回此数据在ComboBox的DataProvider中的下标，
		 * 此下标也即成为SelectedIndex的依据 
		 * @param dataString 待查找的data数据
		 * @param dataProvider 被检索的数据ArrayCollection
		 * @return dataString在DataProvider中的下标，-1表示DataProvider为空，-2表示dataString为空，-3表示没有匹配的记录
		 * 
		 */
		public static function findSelectedItemIdForComboBoxDataProvider(dataString : String, dataProvider : ArrayCollection) : Number
		{
			trace("enterFunction");
			trace("dataProvider:" + dataProvider);
			trace("lenght:" + dataProvider.length);
			if ( dataProvider == null || dataProvider.length == 0 )
			{

				return -1;
			}
			else if ( dataString == null || "" == dataString )
			{
				return -2;
			}
			for ( var iIndex:int = 0 ; iIndex < dataProvider.length ; iIndex++ )
			{
				if ( dataProvider[iIndex].data == dataString )
				{
					trace(iIndex);
					return iIndex;
				}
				trace("found no matched record");
			}
			return -3;
		}
		
		/**
		 *本函数用来寻找ComboBox中是否存在label与传递的label相匹配的记录，
		 * 如果有，则返回此数据在ComboBox的DataProvider中的下标，
		 * 此下标也即成为SelectedIndex的依据 
		 * @param labelString 待查找的data数据
		 * @param dataProvider 被检索的数据ArrayCollection
		 * @return labelString在DataProvider中的下标，-1表示DataProvider为空，-2表示dataString为空，-3表示没有匹配的记录
		 * 
		 */
		public static function findSelectedItemIdForComboBoxDataProviderWithLabel(labelString : String, dataProvider : ArrayCollection) : Number
		{
			trace("enterFunction");
			if ( dataProvider == null || dataProvider.length == 0 )
			{
				return -1;
			}
			else if ( labelString == null || "" == labelString )
			{
				return -2;
			}
			for ( var iIndex:int = 0 ; iIndex < dataProvider.length ; iIndex++ )
			{
				if ( dataProvider[iIndex].label == labelString )
				{
					trace(iIndex);
					return iIndex;
				}
			}
			return -3;
		}		
		/**
		 *本函数通过匹配的dataString数据定位ComboBox的SelectedIndex 
		 * @param dataString 待查找的数据
		 * @param comboBox 需要定位的ComboBox
		 * 
		 */		
		public static function changeComboBoxSelectedIndexToMatchedData(dataString : String, comboBox:ComboBox) : void
		{		
			if ( comboBox && comboBox.dataProvider )
			{
				trace("ready to match");
				var tempSelectedIndex:Number = Number(findSelectedItemIdForComboBoxDataProvider(dataString, comboBox.dataProvider as ArrayCollection));
				   		
				if ( tempSelectedIndex >= 0 )
				{
				   	comboBox.selectedIndex = tempSelectedIndex;	
				}
				else
				{
					//Alert.show("comboBox.selectedLabel is "+comboBox.selectedLabel);
					//Alert.show("请选择"+comboBox.selectedIndex);
					comboBox.prompt = "请选择";
				}
				
			}
		}
//		/**
//		 *本函数通过匹配的dataString数据定位AutoComplete的SelectedIndex 
//		 * @param dataString 待查找的数据
//		 * @param autoComplete 需要定位的AutoComplete
//		 * 
//		 */
//		public static function changeAutoCompleteSelectedIndexToMatchedData(dataString : String, autoComplete:AutoComplete) : void
//		{
//			if ( autoComplete && autoComplete.dataProvider )
//			{
//				trace("ready to match");
//				var tempSelectedIndex:Number = Number(findSelectedItemIdForComboBoxDataProvider(dataString, autoComplete.dataProvider as ArrayCollection));
//				   		
//				if ( tempSelectedIndex >= 0 )
//				{
//					//Alert.show("tempSelectedIndex is ... "+tempSelectedIndex);
//				   	autoComplete.creationIndex = tempSelectedIndex;				   	
//				}
//				else
//				{					
//					autoComplete.prompt = "请选择";
//				}
//				
//			}
//		}
		
		/**
		 *本函数通过匹配的labelString数据定位ComboBox的SelectedIndex 
		 * @param labelString 待查找的数据
		 * @param comboBox 需要定位的ComboBox
		 * 
		 */		
		public static function changeComboBoxSelectedIndexToMatchedLabel(labelString : String, comboBox:ComboBox) : void
		{
			trace(comboBox.dataProvider);
			if ( comboBox && comboBox.dataProvider )
			{
				trace("ready to match");
				var tempSelectedIndex:Number = Number(findSelectedItemIdForComboBoxDataProviderWithLabel(labelString, comboBox.dataProvider as ArrayCollection));
				   		
				if ( tempSelectedIndex >= 0 )
				{
				   	comboBox.selectedIndex = tempSelectedIndex;
				    
				   	
				}
				else
				{
					//Alert.show("comboBox.selectedLabel is "+comboBox.selectedLabel);
					//Alert.show("请选择"+comboBox.selectedIndex);
					comboBox.prompt = "请选择";
				}
				
			}
		}
		
		/* public static function changeAutoCompleteSelectedIndexToMatchedData( dataString : String,autoComplete: AutoComplete) : void
		{
			trace(autoComplete.dataProvider);
			if ( autoComplete && autoComplete.dataProvider )
			{
				trace("ready to match");
				var tempSelectedIndex:Number = Number(findSelectedItemIdForComboBoxDataProviderWithLabel(dataString, autoComplete.dataProvider as ArrayCollection));
				   		
				if ( tempSelectedIndex >= 0 )
				{
				   	autoComplete.combo.creationIndex = tempSelectedIndex;
				    Alert.show("autoComplete.creationIndex is ... "+autoComplete.creationIndex);
				   	
				}
				else
				{
					
					//Alert.show("comboBox.selectedLabel is "+comboBox.selectedLabel);
					Alert.show("no tempSelectedIndex  ... "+tempSelectedIndex);
					autoComplete.text = "";
				}
				
			}
		} */
		
	}
}